<template>
  <!-- $t is vue-i18n global function to translate lang -->
  <div class="app-container">

    <!-- <div> -->
    <!-- <FilenameOption v-model="filename" />
      <AutoWidthOption v-model="autoWidth" /> -->
    <!-- <BookTypeOption v-model="bookType" />
      <el-button :loading="downloadLoading" style="margin:0 0 20px 20px;" type="primary" icon="document" @click="handleDownload">{{ '导出' }} Excel</el-button>
    </div> -->
    <!-- <el-card class="box-card" style="margin-bottom:1%;">
      <BookTypeOption v-model="bookType" />
      <el-button :loading="downloadLoading" style="margin:0 0 20px 20px; padding:7px 10px;" type="primary" icon="document" @click="handleDownload"> -->
    <!-- {{ '导出' }} Excel -->
    <!-- <svg-icon icon-class="export" class-name="export-class" />
      </el-button>
    </el-card> -->
    <el-card ref="box-card" class="box-card">
      <div slot="header" class="titleBox">
        <span>{{ '文章列表' }}</span>
        <div>
          <BookTypeOption v-model="bookType" />
          <el-button :loading="downloadLoading" style=" padding:7px 10px;" type="primary" icon="document" @click="handleDownload">
            <!-- {{ '导出' }} Excel -->
            <svg-icon icon-class="export" class-name="export-class" />
          </el-button>
        </div>
      <!-- <i class="el-icon-search"/> -->
      </div>
      <el-table v-loading="listLoading" :data="list" element-loading-text="拼命加载中" border fit highlight-current-row>
        <el-table-column align="center" label="Id" width="95">
          <template slot-scope="scope">
            {{ scope.$index }}
          </template>
        </el-table-column>
        <el-table-column label="Title">
          <template slot-scope="scope">
            {{ scope.row.title }}
          </template>
        </el-table-column>
        <el-table-column label="Author" width="110" align="center">
          <template slot-scope="scope">
            <el-tag>{{ scope.row.author }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="Readings" width="115" align="center">
          <template slot-scope="scope">
            {{ scope.row.pageviews }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="Date" width="220">
          <template slot-scope="scope">
            <i class="el-icon-time"/>
            <span>{{ scope.row.timestamp }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

  </div>
</template>

<script>
import { fetchList } from '@/api/article'
// import { parseTime } from '@/utils'// | parseTime('{y}-{m}-{d} {h}:{i}')
// options components
import FilenameOption from './components/FilenameOption'
import AutoWidthOption from './components/AutoWidthOption'
import BookTypeOption from './components/BookTypeOption'
export default {
  name: 'ExportExcel',
  components: { FilenameOption, AutoWidthOption, BookTypeOption },
  data() {
    return {
      list: null,
      listLoading: true,
      downloadLoading: false,
      filename: '',
      autoWidth: true,
      bookType: 'xlsx'
    }
  },
  created() {
    this.fetchData()
  },
  mounted() {
    console.log(document.body.clientHeight)
    this.$refs['box-card'].$el.style.minHeight = document.body.clientHeight - 70 - 40 + 'px'
    console.log(this.$refs['box-card'].$el.style.minHeight)
  },
  methods: {
    fetchData() {
      this.listLoading = true
      fetchList().then(response => {
        console.log(response)
        this.list = response.data
        this.listLoading = false
      })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['Id', 'Title', 'Author', 'Readings', 'Date']
        const filterVal = ['id', 'title', 'author', 'pageviews', 'display_time']
        const list = this.list
        const data = this.formatJson(filterVal, list)
        console.log(data)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: this.filename,
          autoWidth: this.autoWidth,
          bookType: this.bookType
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        return v[j]
        // if (j === 'timestamp') {
        //   return parseTime(v[j])
        // } else {
        //   return v[j]
        // }
      }))
    }
  }
}
</script>
<style lang="scss" scoped>
.svg-icon[data-v-c8a70580]{
  width: 20px;
  height: 20px;
}
.titleBox{
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>

<style lang="scss" >
@import "src/styles/variables.scss";
.radio-label {
  font-size: 14px;
  color: #606266;
  line-height: 40px;
  padding: 0 12px 0 30px;
}
.el-button--primary{
  background-color: $mainTone !important;
  border-color: $mainTone;
}

</style>
